import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:life_milestones/features/MS-home-group/MS-index.dart';

import '../util/MS-log-util.dart';
import 'MS-mile-stone-page.dart';
import 'MS-mine-page.dart';

class MSMainPage extends StatefulWidget {
  const MSMainPage({super.key});

  static _MSMainPageState? of(BuildContext context){
    NavigatorState? navigator;
    if (context is StatefulElement && context is MSMainPage) {
      return context.state as _MSMainPageState;
    }
    return context.findAncestorStateOfType<_MSMainPageState>();

  }
  @override
  State<StatefulWidget> createState() {
    return _MSMainPageState();
  }
}

class _MSMainPageState extends State<MSMainPage> {

  int current = 0;
  late PageController controller;
  @override
  void initState() {
    controller =  PageController(initialPage: 0);
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return WillPopScope(child: Scaffold(
      resizeToAvoidBottomInset: false,
      body:SizedBox.expand(child: Stack(
        children: [
          SizedBox.expand(
            child: PageView(
              physics: const NeverScrollableScrollPhysics(),
              controller: controller,
              children:  [
                const MSHomeGroupApp(),
                MileStonePage(),
                const MinePage()
              ],
            ),
          ),
          Align(alignment: Alignment.bottomCenter,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                InkWell(onTap:(){
                  MSLog.addEvent(page:MSLogPages.Home);
                  setState((){
                    current = 0;
                    controller.jumpToPage(0);
                  });
                }, child: Image.asset(current == 0 ? "images/main-left-checked.png" : "images/main-left.png",width: 125.w,fit: BoxFit.fitWidth,)),
                InkWell(onTap:(){
                  MSLog.addEvent(page:MSLogPages.MileStone);
                  setState((){
                    current = 1;
                    controller.jumpToPage(1);

                  });
                }, child: Image.asset(current == 1 ? "images/main-center-checked.png" : "images/main-center.png",width: 125.w,)),
                InkWell(onTap:(){
                  MSLog.addEvent(page:MSLogPages.Mine);
                  setState((){
                    current = 2;
                    controller.jumpToPage(2);

                  });
                }, child: Image.asset(current == 2 ? "images/main-right-checked.png" : "images/main-right.png",width: 125.w,)),
              ],
            ),)
        ],
      ),),
    ), onWillPop: ()async{
      return false;
    });
  }
}
